<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-row :gutter="20">
        <a-col :span="20">
          <a-input v-model="queryParam.UserName" placeholder="请输入姓名" allowClear />
        </a-col>
        <a-col :span="4">
          <a-button type="primary" @click="$refs.rostertable.refresh(true)">搜索</a-button>
        </a-col>
      </a-row>
      <a-divider />
      <s-table
        ref="rostertable"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="record => record.customId"
        :rowSelection="options.rowSelection"
      >
        <span slot="type" slot-scope="text, record">
          {{ typeEnum[record.type] }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => sysPosDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
        <template slot="operator">
          <a-button type="danger" :disabled="!selectedRowKeys.length" @click="batchDetele">删除</a-button>
        </template>
      </s-table>
    </a-spin>
  </a-modal>
</template>
<script>
import { STable, XCard, XDown } from '@/components'
import { deviceData, deleteDeviceData } from '@/api/modular/garden/sound'
export default {
  components: {
    XDown,
    XCard,
    STable
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
      deviceNumber: undefined,
      selectedRowKeys: [],
      selectedRows: [],
      ipAddress: '',
      title:'设备数据',
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: 'IP地址',
          dataIndex: 'ipAddress'
        },
        {
          title: '类型',
          scopedSlots: { customRender: 'type' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return deviceData(Object.assign(parameter, { Number: this.deviceNumber }, this.queryParam)).then(res => {
          return res.data
        })
      },
      typeEnum: {
        1: '学生',
        2: '老师',
        3: '家长'
      },
      queryParam:{
        UserName: undefined,
        type: undefined
      }
    }
  },
  methods: {
    handleSubmit() {},
    handleCancel() {
      this.$refs.rostertable.clearRefreshSelected()
      this.queryParam.UserName = undefined
      this.visible = false
    },
    sysPosDelete() {
      const params = {
        ipAddress: this.ipAddress,
        customIdList: this.selectedRowKeys
      }
      deleteDeviceData(params)
        .then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.rostertable.clearRefreshSelected()
            this.$refs.rostertable.refresh(true)
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
        .catch(err => {
          this.$message.error('删除错误：' + err.message)
        })
    },
    show(record) {
      this.deviceNumber = record.number
      this.ipAddress = record.ipAddress
      this.queryParam.type = record.deviceType
      this.title = '设备数据-'+record.name
      this.visible = true
      this.$nextTick(() => {
        this.$refs.rostertable.refresh(true)
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    batchDetele() {
      this.$confirm({
        title: '提示',
        content: '确定删除设备数据 ?',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          this.sysPosDelete()
          this.confirmLoading = true
          setTimeout(()=>{
            this.confirmLoading=false
          },10000)
        },
        onCancel () {
        }
      })
      
    }
  }
}
</script>
